package com.weslide.weslideapp.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.gt.cl.http.CLConnectionException;
import com.gt.cl.http.CLInvalidNetworkException;
import com.weslide.weslideapp.R;
import com.weslide.weslideapp.WeSlideApplication;
import com.weslide.weslideapp.component.MySideBar;
import com.weslide.weslideapp.component.MySideBar.OnTouchingLetterChangedListener;
import com.weslide.weslideapp.manager.api.APIManager;
import com.weslide.weslideapp.manager.api.LoginManager;
import com.weslide.weslideapp.pojo.ao.ZoneAO;
import com.weslide.weslideapp.pojo.ao.ZoneListAO;
import com.weslide.weslideapp.util.TMTPAsyncTask;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CityListActivity extends WeSlideActivity implements
		OnTouchingLetterChangedListener {
	private TextView overlay;
	private MySideBar mySideBar;
	private ListView mCityListView;
	private List<ZoneAO> cityList = new ArrayList<ZoneAO>();
	private TMTPAsyncTask<Void, Void, Void> asyncTask;
	private ZoneListAO zoneList;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_citylist);
		findViews();
		init();
	}

	private void init() {
		mCityListView.setAdapter(myAdapter);
		asyncTask = new TMTPAsyncTask<Void, Void, Void>(this) {

			@Override
			protected void doOnSuccess(Void result) {
				if (zoneList.getStatus().equals("1")) {
					cityList.addAll(zoneList.getZones());
					myAdapter.notifyDataSetChanged();
				}
			}

			@Override
			protected Void doInBackground(Void params)
					throws CLInvalidNetworkException, CLConnectionException {
				try {
					zoneList = APIManager.getInstance().getZoneList();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
				}
				return null;
			}
		};
		asyncTask.execute();
	}

	private void findViews() {
		overlay = (TextView) findViewById(R.id.tvLetter);
		mySideBar = (MySideBar) findViewById(R.id.myView);
		mCityListView = (ListView) findViewById(R.id.cityList);
		mCityListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ZoneAO zoneAO = cityList.get(position);
				LoginManager.getInstance().setUpCurrentCity(zoneAO);
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	@Override
	public void onTouchingLetterChanged(String s) {
		overlay.setText(s);
		// overlay.setVisibility(View.VISIBLE);
		if (alphaIndexer(s) >= 0) {
			int position = alphaIndexer(s);
			mCityListView.setSelection(position);
		}
	}

	public int alphaIndexer(String s) {
		int position = 0;
		for (int i = 0; i < cityList.size(); i++) {
			if (cityList.get(i).getEnglishChar().startsWith(s)) {
				position = i;
				return position;
			}
		}
		return position;
	}

	private BaseAdapter myAdapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.item_city,
						null);
				holder = new ViewHolder();
				holder.mTvCatalog = (TextView) convertView
						.findViewById(R.id.item_city_mTvCatalog);
				holder.mTvCity = (TextView) convertView
						.findViewById(R.id.item_city_mTvCity);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ZoneAO zoneAO = cityList.get(position);
			String catalog = zoneAO.getEnglishChar();
			holder.mTvCatalog.setText(catalog);
			holder.mTvCity.setText(zoneAO.getName());
			return convertView;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public int getCount() {
			return cityList.size();
		}
	};

	class ViewHolder {
		TextView mTvCatalog;
		TextView mTvCity;
	}

}
